//
//  MainTabBarController.m
//  XPhoto
//
//  Created by baboy on 2019/2/20.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "MainTabBarController.h"
#import "UITabBarController+x.h"
#import "UIViewController+version.h"
#import "CameraViewController.h"
#import "BKNavigationController.h"
#import "BKActionSheet.h"
#import "PHotoLibViewController.h"
#import "MediaEditViewController.h"
#import "PhotoCameraViewController.h"
#import "BKSoloNavigationController.h"
#import "UIViewController+camera.h"
#import "SerialSearchViewController.h"
#import "LoginViewController.h"
#import <BKKit/DBCache.h>
#import "LaunchView.h"
#import <BKKit/BKProgressBar.h>

@interface MainTabBarController ()<UITabBarControllerDelegate,BKActionSheetDelegate,PhotoLibViewDelegate,PhotoEditDelegate, LaunchViewDelegate>
@property(nonatomic, strong) LaunchView *launchView;
@property(nonatomic, strong) UIWindow *loginWindow;
@end

@implementation MainTabBarController
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]  removeObserver:self];
}
- (id)init{
    if (self = [super init]) {
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userLogin:) name:NOTIFICATION_USER_LOGIN object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userLogout:) name:NOTIFICATION_USER_LOGOUT object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(configFinished:) name:NOTIFICATION_CONFIG_FINISHED object:nil];
    self.delegate = self;
    [self checkAppViersion];
    if (!_launchView) {
        _launchView = [LaunchView view];
        _launchView.delegate = self;
        _launchView.frame = self.view.bounds;
        [self.view addSubview:_launchView];
    }
    if ([User authToken]) {
        [self refreshToken];
    }
    [self loadViewControllers];
//    CircleProgressBar *progressBar = [[CircleProgressBar alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];
//    progressBar.solid = YES;
//    progressBar.progress = 0.5;
//    [self.view addSubview:progressBar];
}
- (void)login{
    LoginViewController *vc = [[LoginViewController alloc] init];
    BKNavigationController *nav = [[BKNavigationController alloc] initWithRootViewController:vc];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:nav animated:NO completion:nil];
}

- (void)refreshToken{
    [User refreshTokenWithUser:[User currentUser]
                      callback:^(id  _Nonnull task, User *user, NSError * _Nullable error) {
        if(error){
            [BKIndicator showMessageAndFadeOut:error.localizedDescription completion:nil];
            [self login];
            return;
        }
        if (user) {
            [User login:user];
            [User loginWithAuthToken:user.authToken];
        }
    }];
}
- (void)userLogin:(NSNotification *)noti{
//    [self loadViewControllers];
//    [self.view.window makeKeyAndVisible];
}
- (void)userLogout:(NSNotification *)noti{
    [self login];
}
- (void)configFinished:(NSNotification *)noti{
    
}
- (void)launchViewDidDismissed:(LaunchView *)view{
//    [self login];
    if (![User authToken]) {
        [self login];
    }
}
- (UIViewController *)createViewController:(Class )class withTitle:(NSString *)title{
    if(!class){
        return nil;
    }
    UIViewController *vc = [[class alloc] init];
    if([vc isKindOfClass:[CameraViewController class]]){
        return vc;
    }
    [vc setTitle:title];
    BKSoloNavigationController *nav = [[BKSoloNavigationController alloc] initWithRootViewController:vc];
//    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    return nav;
}
- (void)loadViewControllers{
    NSArray *menus = @[
                       @{
                           @"enable":[NSNumber numberWithBool:YES],
                           @"title":NSLocalizedString(@"home", @"标题"),
                           @"icon":@"",
                           @"icon-high-light":@"",
                           @"controller":@"HomeViewController"
                           },
                       @{
                           @"enable":[NSNumber numberWithBool:YES],
                           @"title":NSLocalizedString(@"explore", @"标题"),
                           @"icon":@"",
                           @"icon-high-light":@"",
                           @"controller":@"ExploreViewController"
                           },
                       @{
                           @"enable":[NSNumber numberWithBool:YES],
                           @"title":NSLocalizedString(@"camera", @"标题"),
                           @"icon":@"",
                           @"icon-high-light":@"",
                           @"controller":@"CameraViewController"
                           },
                       @{
                           @"enable":[NSNumber numberWithBool:YES],
                           @"title":NSLocalizedString(@"event", @"标题"),
                           @"icon":@"",
                           @"icon-high-light":@"",
                           @"controller":@"EventViewController"
                           },
                       @{
                           @"enable":[NSNumber numberWithBool:YES],
                           @"title":NSLocalizedString(@"me", @"标题"),
                           @"icon":@"",
                           @"icon-high-light":@"",
                           @"controller":@"MeProfileViewController"
                           }
                       ];
    NSArray * vcs = [self loadViewControllers:menus];
    [self setViewControllers:vcs];
    
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(nonnull UIViewController *)viewController{
    if (![viewController isKindOfClass:[CameraViewController class]]) {
        return YES;
    }
    
    [self takePhoto];
//    SerialSearchViewController *vc = [[SerialSearchViewController alloc] init];
//    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
//    nav.modalPresentationStyle = UIModalPresentationFullScreen;
//    [self presentViewController:nav animated:YES completion:nil];
//    return NO;
//    CameraViewController *vc = [[CameraViewController alloc] init];
//    BKNavigationController * nav = [[BKNavigationController alloc] initWithRootViewController:vc];
//    [self presentViewController:nav
//                       animated:YES
//                     completion:^{
//
//                     }];
    return NO;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end
